package com.huang.forkjoin;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.LongStream;

public class Test {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        t1();
        //t2();
        t3();
    }
    public static void t1(){
        long sum=0;
        long start =System.currentTimeMillis();
        for (long i = 1l; i <= 10_0000_0000; i++) {
            sum+=i;
        }
        long end =System.currentTimeMillis();
        System.out.println("第一种"+sum+"---"+(end-start));
    }
    public static void t2() throws ExecutionException, InterruptedException {
        long start =System.currentTimeMillis();
        ForkJoinPool forkJoinPool = new ForkJoinPool();
        ForkJoinDemo task = new ForkJoinDemo(0L, 10_0000_0000L);
        ForkJoinTask<Long> submit = forkJoinPool.submit(task);
        long sum=submit.get();


        long end =System.currentTimeMillis();
        System.out.println("第二种"+sum+"---"+(end-start));
    }
    public static void t3(){
        /*\
        *  并行流计算
        * */
        Long start =System.currentTimeMillis();
        Long sum=LongStream.rangeClosed(0L,10_0000_0000L)
                   .parallel().reduce(0,Long::sum);


        long end =System.currentTimeMillis();
        System.out.println("第二种"+sum+"---"+(end-start));
    }
}
